#include <QCoreApplication>
#include <iostream>
#include <QProcess>
#include <QStringList>
#include <QFile>
#include <QTextStream>
#include <QDir>
#include <QDate>
#include <QTime>
#include <QFileInfo>
#include <cstdlib>

#include "LBuildVersion.h"

using namespace std;

//----------------------------------------------------------------------------------------
LBuildVersion::LBuildVersion() {

}

//----------------------------------------------------------------------------------------
int LBuildVersion::getBuildNumber() {
    const QDate today = QDate::currentDate();
    const QString curDateBuild = today.toString("yyMMdd");
    return curDateBuild.toInt();
}

//----------------------------------------------------------------------------------------
int LBuildVersion::getSubversionRevision(QString dirPrj) {
    int revision = 0;
    QProcess process;
    process.start("svnversion",
                  QStringList()
//                  << "."
                  << "--no-newline"
                  << dirPrj);
    if (process.waitForStarted() && process.waitForReadyRead()) {
        const QString str(process.readAll().constData());
        const int pos = str.indexOf(':');
        if (pos != -1) {
            revision = atoi(str.mid(pos + 1).toAscii().constData());
        }
        else {
            revision = atoi(str.toAscii().constData());
        }
        process.waitForFinished();
    }
    return revision;
}

//----------------------------------------------------------------------------------------
int LBuildVersion::writeFile(const QString& fileName,
                             const int major,
                             const int minor,
                             const int revision,
                             const int build) {

    QFile tempFile(QDir::toNativeSeparators(fileName));
    if (tempFile.open(QIODevice::WriteOnly)) {
        QTextStream out(&tempFile);
        out << "#ifndef VERSION_H\r\n";
        out << "#define VERSION_H\r\n\r\n";
        out << "namespace Version\r\n";
        out << "{\r\n";
        out << "\tstatic const int MAJOR = " << major << ";\r\n";
        out << "\tstatic const int MINOR = " << minor << ";\r\n";
        out << "\tstatic const int REVISION = " << revision << ";\r\n";
        out << "\tstatic const int BUILD = " << build << ";\r\n";
        out << "}\r\n\r\n";
        out << "#endif // VERSION_H\r\n";

        tempFile.close();

        return 0;
    }
    else {
        cout << "error" << endl;
        cout << "Error creating file!" << endl;
        return 1;
    }
}

//----------------------------------------------------------------------------------------
void LBuildVersion::setVersionMajor(const int ver) {
    major = ver;
}

//----------------------------------------------------------------------------------------
void LBuildVersion::setVersionMinor(const int ver) {
    minor = ver;
}

//----------------------------------------------------------------------------------------
int LBuildVersion::generateVersion(const int verMajor,
                                   const int verMinor,
                                   QString svnRootDir,
                                   QString versionFilePath) {
    setVersionMajor(verMajor);
    setVersionMinor(verMinor);

    revision = getSubversionRevision(QDir::toNativeSeparators(svnRootDir));
    build    = getBuildNumber();

    return writeFile(versionFilePath, major, minor, revision, build);
}

//----------------------------------------------------------------------------------------
